<!--
 * @Description: 
 * @Author: xuzp4
-->
<template>
  <el-dialog
    class="toast-dialog"
    :visible="_visible"
    :close-on-click-modal="false"
    :title="
      type === 'add'
        ? $t('addTip')
        : $t('edit')
    "
    :append-to-body="true"
    width="520px"
    :before-close="handleClose"
  >
    <div class="content d-flex jc-center ai-center">
      <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
        <el-form-item :label="$t('renxiangkuguanli.lib_name')" prop="lib_name">
          <el-input v-model="formData.lib_name" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <MyButton class="cancel" @click="cancel">{{
        $t("common.cancel")
      }}</MyButton>
      <MyButton class="confirm" @click="confirm">{{
        $t("common.confirm")
      }}</MyButton>
    </div>
  </el-dialog>
</template>

<script>
import { addLib, editLib } from "./service";
import util from "@/utils";
import MyButton from "@/components/Button";
export default {
  components: { MyButton },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    row: {
      type: Object,
      default: () => {
        return {};
      },
    },
    type: {
      type: String,
      default: "add",
    },
  },
  watch: {
    row: {
      handler(val) {
        if (val) {
          this.$set(this, "formData", {
            ...val,
          });
        }
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {
    _visible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },
  data() {
    return {
      lib_type_list: [
        { label: this.$t('renxiangkuguanli.blackLib'), value: 1 },
        { label: this.$t('renxiangkuguanli.whiteLib'), value: 2 },
      ],
      formData: {},
      rules: {
        lib_name: [
          {
            required: true,
            message: this.$t("renxiangkuguanli.lib_name_required"),
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    confirm() {
      if (this.type === "add") {
        this.$refs.form.validate((valid) => {
          if (valid) {
            const params = {
              ...this.formData,
            };
            addLib(params).then(() => {
              util.msg.success("add_success");
              this.$emit("refresh");
              this.cancel();
            });
          }
        });
      } else {
        this.$refs.form.validate((valid) => {
          if (valid) {
            const params = {
              ...this.formData,
            };
            editLib(params).then(() => {
              util.msg.success("edit_success");
              this.$emit("refresh");
              this.cancel();
            });
          }
        });
      }
    },
    cancel() {
      this._visible = false;
      this.$refs.form.resetFields();
    },
    handleClose(done) {
      this._visible = false;
      this.$refs.form.resetFields();
      done();
    },
  },
};
</script>
<style lang="less" scoped>
.toast-dialog {
  z-index: 9999;
  .content {
    width: 100%;
    /deep/ .el-form {
      width: 100%;
      .el-input {
        width: 200px;
      }
    }
  }
}
</style>
